﻿#include "log_tool.h"
#include "ipc_server.h"
#include <Windows.h>

int main()
{
    // 创建允许普通用户访问的安全描述符
    SECURITY_ATTRIBUTES saMutex = {0};
    SECURITY_DESCRIPTOR sdMutex;
    InitializeSecurityDescriptor(&sdMutex, SECURITY_DESCRIPTOR_REVISION);
    SetSecurityDescriptorDacl(&sdMutex, TRUE, NULL, FALSE);

    saMutex.nLength = sizeof(SECURITY_ATTRIBUTES);
    saMutex.lpSecurityDescriptor = &sdMutex;
    saMutex.bInheritHandle = FALSE;

    // 创建互斥体检测程序实例, 防止重复启动
    HANDLE hMutex = CreateMutexA(&saMutex, TRUE, "Local\\MengYuServerMutex");
    DWORD last_error = GetLastError();
    if (hMutex == NULL ||
        last_error == ERROR_ALREADY_EXISTS ||
        last_error == ERROR_ACCESS_DENIED)
    {
        if (hMutex)
        {
            ReleaseMutex(hMutex);
            CloseHandle(hMutex);
        }
        return 0;
    }

    LogTool::getInstance()->init("./",LogLevel::LOG_LEVEL_ERROR);

    IpcServer server;
    server.start(); // 启动服务

    return 0;
}